﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using mfe.bll;

namespace mfe_proj.Forms
{
    public partial class MovieForm : BaseDetailForm
    {
        string _ItemGuidID;
        public ItemMovie movie = null;
        public string ItemGuidID { get { return _ItemGuidID; } set { _ItemGuidID = value; } }

        public MovieForm()
        {
            InitializeComponent();
        }

        void LoadCountryList()
        {
            if (Program.CountryCollection == null) Program.CountryCollection = Country.CountryCollection(null, null);

            cbCountry.DataSource = Program.CountryCollection;
            cbCountry.DisplayMember = "Name";
            cbCountry.ValueMember = "ID";
            cbCountry.SelectedIndex = -1;
        }

        public void LoadData()
        {
            LoadCountryList();

            //edit
            if (ItemGuidID != null && ItemGuidID != string.Empty)
            {
                //general
                movie = new ItemMovie(new Guid(ItemGuidID));

                ymdReleased.Year = (int?)movie.ReleasedYear;
                ymdReleased.Month = (int?)movie.ReleasedMonth;
                ymdReleased.Day = (int?)movie.ReleasedDay;
                txtTitle1.Text = movie.Title1 != null ? movie.Title1 : string.Empty;
                txtTitle2.Text = movie.Title2 != null ? movie.Title2 : string.Empty;
                txtDirector.Text = movie.Director != null ? movie.Director : string.Empty;
                txtScreenplayBy.Text = movie.PlayWrittenBy != null ? movie.PlayWrittenBy : string.Empty;
                txtStars.Text = movie.Stars != null ? movie.Stars : string.Empty;
                txtOfficialUrl.Text = movie.WebsiteUrl != null ? movie.WebsiteUrl : string.Empty;
                txtLanguage.Text = movie.Language != null ? movie.Language : string.Empty;
                txtIMDB.Text = movie.IMDB != null ? movie.IMDB : string.Empty;
                txtCategory.Text = movie.Category != null ? movie.Category : string.Empty;
                if (movie.CountryID > 0)
                    cbCountry.SelectedValue = movie.CountryID;
            }
        }

        public override string ValidateData()
        {
            return base.ValidateData();
        }

        public override bool SaveData(string itemGuidID)
        {
            bool retval = false;
            try
            {
                //general
                if (movie == null)
                {
                    movie = new ItemMovie();
                }
                movie.ItemGuidID = itemGuidID;
                movie.Director = txtDirector.Text.Trim();
                movie.PlayWrittenBy = txtScreenplayBy.Text.Trim();
                movie.WebsiteUrl = txtOfficialUrl.Text.Trim();
                movie.Stars = txtStars.Text.Trim();
                movie.Title1 = txtTitle1.Text.Trim();
                movie.Title2 = txtTitle2.Text.Trim();
                movie.IMDB = txtIMDB.Text.Trim();
                movie.Category = txtCategory.Text.Trim();
                movie.Language = txtLanguage.Text.Trim();
                if (cbCountry.SelectedIndex > -1)
                    movie.CountryID = int.Parse(cbCountry.SelectedValue.ToString());
                if (ymdReleased.Year.HasValue)
                    movie.ReleasedYear = ymdReleased.Year.Value;
                if (ymdReleased.Month.HasValue)
                    movie.ReleasedMonth = ymdReleased.Month.Value;
                if (ymdReleased.Day.HasValue)
                    movie.ReleasedDay = ymdReleased.Day.Value;
                //save
                retval = (movie.MovieID == 0) ? movie.Save() : movie.Update();
            }
            catch (Exception ex)
            {
                mfe.lib.Logger.Log(ex);
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return retval;   
        }
        
        public Panel DockablePanel
        {
            get { return panel1; }
            set { panel1 = value; }
        }
    }
}
